package com.ulk.systempermission;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.ulk.corelib.base.BaseActivity;
import com.ulk.corelib.db.Packet;
import com.ulk.corelib.utils.apk.ApkUtils;
import com.ulk.systempermission.utils.PowerUtils;
import com.ulk.systempermission_interface.SystemPermissionDefine;

public class MainActivity extends BaseActivity {

    private static final String TAG = "MainActivity";

    private Button mButtonStartService = null;
    private Button mButtonStopService = null;
    private Button mButtonGotoSleep = null;
    private Button mButtonWakeup = null;

    @Override
    public String getMessageType() {
        return SystemPermissionDefine.MODULE;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButtonStartService = (Button) findViewById(R.id.start_service);
        mButtonStartService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean ret = false;
                ret = ApkUtils.startServiceSafety(MainActivity.this, SystemPermissionDefine.FLOAT_WINDOW_SERVICE_NAME,
                        SystemPermissionDefine.FLOAT_WINDOW_SERVICE_PACKET_NAME,
                        SystemPermissionDefine.FLOAT_WINDOW_SERVICE_CLASS_NAME);
                if (ret) {
                    Toast.makeText(MainActivity.this, "系统权限服务已启动！", Toast.LENGTH_SHORT).show();
                }
            }
        });

        mButtonStopService = (Button) findViewById(R.id.stop_service);
        mButtonStopService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean ret = false;
                ret = ApkUtils.stopServiceSafety(MainActivity.this, SystemPermissionDefine.FLOAT_WINDOW_SERVICE_NAME,
                        SystemPermissionDefine.FLOAT_WINDOW_SERVICE_PACKET_NAME,
                        SystemPermissionDefine.FLOAT_WINDOW_SERVICE_CLASS_NAME);
                if (ret) {
                    Toast.makeText(MainActivity.this, "系统权限服务已停止！", Toast.LENGTH_SHORT).show();
                }
            }
        });

        mButtonGotoSleep = (Button) findViewById(R.id.goto_sleep);
        mButtonGotoSleep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Boolean isRoot = false;
//                ShellUtils.CommandResult sc = ShellUtils.execCommand("echo mem > /sys/power/state", isRoot, true);
//                LogUtils.d(TAG, "sc.successMsg = " + sc.successMsg + " sc = errorMsg" + sc.errorMsg + " result = " + sc.result);

                Packet packet = new Packet();
                String[] strings = new String[11];
                strings[0] = "com.ulk.main";
                strings[1] = "com.ulk.accoff";
                strings[2] = "com.ulk.systempermission";
                strings[3] = "com.android.smspush";
                strings[4] = "android.process.acore";
                strings[5] = "com.android.externalstorage";
                strings[6] = "com.android.cellbroadcastreceiver";
                strings[7] = "com.android.dm";
                strings[8] = "com.android.systemui";
                strings[9] = "android.process.media";
                strings[10] = "com.android.onetimeinitializer";
                packet.putStringArray("KeepPackages", strings);
                PowerUtils.goToDeepSleep(MainActivity.this, packet);
            }
        });

        mButtonWakeup = (Button) findViewById(R.id.wakeup);
        mButtonWakeup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Boolean isRoot = false;
//                ShellUtils.CommandResult sc = ShellUtils.execCommand("echo on > /sys/power/state", isRoot, true);
//                LogUtils.d(TAG, "sc.successMsg = " + sc.successMsg + " sc = errorMsg" + sc.errorMsg + " result = " + sc.result);

                PowerUtils.wakeupFromDeepSleep(MainActivity.this, null);
            }
        });
    }
}
